﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment2_OrderSystem
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString.Count > 0)
                {
                    if (Request.QueryString["SignedUser"] == "true")
                    {
                        PageSignedUser();
                    }
                    else if (Request.QueryString["NewUser"] == "true")
                    {
                        PageNewUser();
                    }
                    else if (Request.QueryString["LogOut"] == "true")
                    {
                        LogOutFromPage();
                    }
                }
                else if (Request.Cookies["ClientID"] != null)
                {
                    PageHasCookies();
                }
            }
        }
        private void ShoWelcomeLabels()
        {
            anonymousUserOptions.Attributes.CssStyle.Value = "display: none;";
            registerdUserOptions.Attributes.CssStyle.Value = "display: inline;";
        }
        protected void LogOut_Click(object sender, EventArgs e)
        {
            string logout = "Home.aspx?LogOut=true";
            Response.Redirect(logout, false);
        }
        private void AddCookies()
        {
            // Add cookies
            HttpCookie TimeCookie = new HttpCookie("LastEntered", DateTime.Now.ToString());
            HttpCookie IDCookie = new HttpCookie("ClientID", Request.QueryString["ClientID"].ToString());
            HttpCookie UserCookie = new HttpCookie("Username", Request.QueryString["Username"].ToString());
            TimeCookie.Expires = DateTime.Now.AddDays(100);
            IDCookie.Expires = DateTime.Now.AddDays(100);
            UserCookie.Expires = DateTime.Now.AddDays(100);
            Response.Cookies.Add(IDCookie);
            Response.Cookies.Add(UserCookie);
            Response.Cookies.Add(TimeCookie);
        }
        private void PageNewUser()
        {
            ShoWelcomeLabels();
            aHelloUser.InnerHtml += Request.QueryString["Username"].ToString();
            aLastEntered.InnerHtml += "Just Now";
            // Saving the cliend id for this session
            Session["CliendID"] = Request.QueryString["ClientID"];
            // Update time cookie
            Response.Cookies["LastEntered"].Value = DateTime.Now.ToString();
            // Load the favorites products
        }
        private void PageSignedUser()
        {
            ShoWelcomeLabels();
            aHelloUser.InnerHtml += Request.QueryString["Username"].ToString();
            if (Request.Cookies["LastEntered"] != null)
            {
                aLastEntered.InnerHtml += Request.Cookies["LastEntered"].Value;
            }
            else
            {
                AddCookies();
                aLastEntered.InnerHtml += DateTime.Now;
            }
            // Saving the cliend id for this session
            Session["CliendID"] = Request.QueryString["ClientID"];
            // Update time cookie
            Response.Cookies["LastEntered"].Value = DateTime.Now.ToString();
            // Load the favorites products
        }
        private void PageHasCookies()
        {
            ShoWelcomeLabels();
            aHelloUser.InnerHtml += Request.Cookies["Username"].Value.ToString();
            aLastEntered.InnerHtml += Request.Cookies["LastEntered"].Value;
            // Update time cookie
            Response.Cookies["LastEntered"].Value = DateTime.Now.ToString();
            // Load the favorites products
        }
        private void LogOutFromPage()
        {
            // End the current session
            Session.Abandon();
            // Replace the welcome screen label
            anonymousUserOptions.Attributes.CssStyle.Value = "display: inline;";
            registerdUserOptions.Attributes.CssStyle.Value = "display: none;";
        }
    }
}